package main

import (
	"encoding/json"
	"fmt"
)

type ChapterJson struct {
}

type DebugInfo struct {
	Level, Msg, author string
	//author 小写不会被json序列化
}

type DebugLabel struct {
	Level string `json:"_level"`
	// - 不会被序列化
	Author string `json:"-"`
	Msg    string
}
type ChapterLog struct {
	DebugInfo
	Class string
}

func main() {
	//map 转json
	rating := map[string]float64{
		"c++":    5.0,
		"java":   7.0,
		"python": 8.0,
		"go":     9.0,
	}

	data, err := json.Marshal(rating)
	chapterJson := ChapterJson{}
	chapterJson.printJsonError(err, "map转json失败,失败原因:")

	fmt.Printf("map 转json ,json 为 %s ,\n ", data)
	//json 带缩进格式
	indent, err := json.MarshalIndent(rating, "", " ")
	chapterJson.printJsonError(err, "map转json带缩进失败,失败原因:")
	fmt.Printf("map 转json带缩进 ,json 为 %s ,\n ", indent)
	//结构体转换为json
	debugInfo := DebugInfo{"debug", "not found", "万祥"}
	debugJson, err := json.MarshalIndent(debugInfo, "", " ")
	chapterJson.printJsonError(err, "struct 转json带缩进失败,失败原因:")
	fmt.Printf("struct 转json带缩进 ,json 为 %s ,\n ", debugJson)
	//带标签的结构体转化为json
	label := DebugLabel{"debug", "万祥", "not found"}
	labelJson, err := json.MarshalIndent(label, "", " ")
	chapterJson.printJsonError(err, "label 转json带缩进失败,失败原因:")
	fmt.Printf("label 转json带缩进 ,json 为 %s ,\n ", labelJson)
	//解析匿名字段时候,会将匿名字段的字段当成该结构体的字段处理
	chapterLog := ChapterLog{DebugInfo{"debug", "not found", "万祥"}, "main.chapter-json"}
	chapterLogJson, err := json.MarshalIndent(chapterLog, "", " ")
	chapterJson.printJsonError(err, "匿名字段 转json带缩进失败,失败原因:")
	fmt.Printf("匿名字段 转json带缩进 ,json 为 %s ,\n ", chapterLogJson)
	//字符串解析为map
	str := `{"c++":5,"go":9,"java":7,"python":8}`
	var ratingMap map[string]float64
	err = json.Unmarshal([]byte(str), &ratingMap)
	chapterJson.printJsonError(err, "json转换为 map 失败,失败原因:")
	fmt.Printf("ratingMap == %v \n ", ratingMap)
	//字符串解析为 结构体
	str = `{
	 "Level": "debug",
	 "Msg": "not found",
	 "author": "万祥"
	}`
	var debugInfoStruct DebugInfo
	err = json.Unmarshal([]byte(str), &debugInfoStruct)
	chapterJson.printJsonError(err, "json转换为 结构体 失败,失败原因:")
	fmt.Printf("debugInfoStruct level == %s , msg == %s , author == %s  \n ", debugInfoStruct.Level, debugInfoStruct.Msg, debugInfoStruct.author)
	//标签字符串解析为 结构体
	str = `{
	 "_level": "debug",
	 "Msg": "not found",
     "Author": "万祥"
	} `
	var debugLabel2 DebugLabel
	err = json.Unmarshal([]byte(str), &debugLabel2)
	chapterJson.printJsonError(err, "json转换为 带标签结构体 失败,失败原因:")
	fmt.Printf("debugLabel2 level == %s , msg == %s , author == %s  \n ", debugLabel2.Level, debugLabel2.Msg, debugLabel2.Author)
	// 匿名字段字符串 解析为 结构体
	str = ` {
 "Level": "debug",
 "Msg": "not found",
 "Class": "main.chapter-json"
} `
	var log ChapterLog
	err = json.Unmarshal([]byte(str), &log)
	chapterJson.printJsonError(err, "json转换为 匿名字段结构体 失败,失败原因:")
	fmt.Printf("log level == %s , msg == %s , author == %s  \n ", log.DebugInfo.Level, log.Msg, log.Class)

}

func (c *ChapterJson) printJsonError(err error, msg string) {
	if err != nil {
		fmt.Println(msg, err.Error())
		return
	}
}
